<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :before-close="onCancel" width="769px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户昵称">
							<el-input v-model="state.ruleForm.nickName" placeholder="请输入用户昵称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="关联角色">
							<el-select
								v-model="state.ruleForm.authRole"
								filterable
								multiple
								placeholder="请选择"
								clearable
								class="w100"
							>
								<el-option
									v-for="item in state.roleData"
									:key="item.roleKey"
									:label="item.roleName"
									:value="item.roleKey"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号">
							<el-input v-model="state.ruleForm.phone" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="密码">
							<el-input v-model="state.ruleForm.password" placeholder="不输入表示不修改密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
							<el-switch v-model="state.status" inline-prompt active-text="启" inactive-text="禁" @change="statusChange"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref} from 'vue';
import { ElMessage } from 'element-plus';
import { useUserApi } from '/@/api/user';

// 引入 api 请求接口
const userApi = useUserApi();

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		nickName: '', // 用户昵称
		avatar: '', // 头像
		phone: '', // 手机号
		isNoPower: false, // 禁用
		authRole: [] as string[],
		password: '',
	},
	status: true,
	roleData: [] as RowRoleType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const backupData = JSON.parse(JSON.stringify(state.ruleForm))

// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.status = !state.ruleForm.isNoPower
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	userDialogFormRef.value.resetFields();
	state.ruleForm = backupData
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') {
		userApi.addUser(state.ruleForm).then((res) => {
			onSubmitHandle(res)
		})
	} else {
		userApi.editUser(state.ruleForm).then((res) => {
			onSubmitHandle(res)
		})
	}
};


const onSubmitHandle = (res: any) => {
	// 刷新菜单
	if (res.id == 0) {
		ElMessage.error('操作失败');
		return false;
	}
	closeDialog(); // 关闭弹窗
	emit('refresh');
}

// 初始化角色数据
const setRoleData = (roleData: RowRoleType[]) => {
	state.roleData = roleData;
};
const statusChange = () => {
	state.ruleForm.isNoPower = !state.status
}
// 暴露变量
defineExpose({
	openDialog,
	setRoleData,
});
</script>
